package com.etc;


public class SingleObject {

    /**
     * 懒汉式，线程不安全
     */
//    private static SingleObject singleObject = null;
//
//    private SingleObject(){}
//
//    public static synchronized  SingleObject getInstance(){
//        if (singleObject == null){
//            singleObject = new SingleObject();
//        }
//        return singleObject;
//    }

    /**
     * 饿汉式
     */
//    private static SingleObject singleObject = new SingleObject();
//
//    private SingleObject(){}
//
//    private static SingleObject getInstance(){
//        return singleObject;
//    }


    /**
     * 双检锁/双重校验锁（DCL，即 double-checked locking）
     */

    private volatile static SingleObject singleObject;

    private SingleObject(){}

    public static SingleObject getSingleton() {

        if (singleObject == null){
            synchronized (SingleObject.class){
                if (singleObject == null){
                    singleObject = new SingleObject();
                }
            }
        }
        return singleObject;
    }



}
